﻿using System.CodeDom;
using System.Collections.Generic;
using BMC.ARSystem;

namespace Wicresoft.EKA.NRemedy.CodeGenerate
{
   public class CSharpModelGenerateCode
    {
        public CodeCompileUnit GenerateClassByFormName(IARSchema asrsch, string namespacem, string formName, List<uint> ids, CSharpModelGeneratorDefaultFactory csmgdf)
        {
            CodeCompileUnit ccu = new CodeCompileUnit();
            IGenerateNameResolver gnr = csmgdf.CreateGenerateNameResolver();
            //root namespace
            IGenerateRootNamespace rn = csmgdf.CreateGenerateRootNamespace(namespacem);
            rn.Create(ccu);
            //import namespace
            IGenerateImportNamespace imn = csmgdf.CreateGenerateImportNamespace();
            imn.Create(ccu.Namespaces[0]);
            //class
            IGenerateClass gc = csmgdf.CreateGenerateClass();
            gc.Create(ccu.Namespaces[0], gnr.DefaultClassNameResolver(formName), formName);
            //field
            var fields = asrsch.GetListField(formName);
            List<BMC.ARSystem.Field> fieldSelected = new List<BMC.ARSystem.Field>();
            foreach (var f0 in ids)
            {
                fieldSelected.Add(asrsch.GetField(formName, f0));
            }

            fieldSelected.Sort(new FieldComparer());
            //property
            Dictionary<string, object> mapping = new Dictionary<string, object>();
            foreach (var f2 in fieldSelected)
            {
                if (f2.Id != 15)
                {
                    mapping.Clear();
                    mapping.Add("DatabaseID", f2.Id);
                    mapping.Add("DatabaseName", f2.Name);
                    mapping.Add("DataType", (ARType)f2.DataType);
                    BMC.ARSystem.Limit limit = f2.Limit;
                    IGeneratePropertyFactory igpf = csmgdf.CreateGeneratePropertyFactory();
                    IGenerateProperty igp = igpf.getGeneratePropertyCreate((ARType)f2.DataType);
                    igp.Create(ccu.Namespaces[0].Types[0], gnr.DefaultPropertyNameResolver(f2.Name), ARTypeConvert.MappingARType(f2.DataType).Name,
                        limit, mapping);
                }
                else
                {
                    mapping.Clear();
                    mapping.Add("DatabaseID", f2.Id);
                    mapping.Add("DatabaseName", f2.Name);
                    mapping.Add("DataType", (ARType)f2.DataType);
                    BMC.ARSystem.Limit limit = f2.Limit;
                    IGenerateProperty igp = new GenerateStatusHistoryProperty();
                    igp.Create(ccu.Namespaces[0].Types[0], gnr.DefaultPropertyNameResolver(f2.Name), typeof(StatusHistory).Name,
                        limit, mapping);
                }

            }

            CodeConstructor defaultConstructor = new CodeConstructor();
            defaultConstructor.Attributes = MemberAttributes.Public;
            defaultConstructor.Statements.Add(new CodeAssignStatement(new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "FormName"),
                new CodePrimitiveExpression(formName)));
            ccu.Namespaces[0].Types[0].Members.Add(defaultConstructor);
            return ccu;
        }
    }
}
